package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.ResHead;
import com.handinfo.android.core.resource.ResourceManager;
import com.handinfo.android.game.Attribute;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.item.Pet;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWDragListener;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWAnimationBox;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWGrids;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIPet implements IUIWindows
{

	public static DWFrame m_pet_frame = null;
	private DWTitle m_pet_title = null;
	private DWImageBox m_imageBox_title = null;
	private Bitmap m_bm_title = null;
	private DWBackground m_pet_background = null;
	private DWBackground m_background_left = null;
	private DWBackground m_background_right = null;
	private Bitmap m_backgrpund_bitmap = null;
	
	private DWImageBox[] m_grid_ditu = new DWImageBox[20];

	public static DWButton m_pet_bt01 = null;//收回
	public static DWButton m_pet_bt02 = null;//背包
	public static DWButton m_pet_shangcheng = null;
//	public static DWImageBox m_pet_kuang = null;
	private Bitmap m_bm_kuang = null;
	public  DWGrids m_pet_gd = null;
	private static DWGrids m_grid_beijing = null;//因为pet不是item所以下面要衬图
	private DWTextbox m_text_next = null;//下一等级
	private DWLabel m_pet_label = null;
	private static Bitmap[] m_pet_bm = new Bitmap[11];
	public DWImageBox m_pet_close = null;
	private Bitmap[] m_pet_tubiao = new Bitmap[20];
	private Bitmap m_bm_anxia = null;
	//消息确认框
	private DWMessageBox m_pet_messageBox = null;

	public static DWListbox listBox =null;
	public static DWTextbox m_pet_shangchengtishi =null; //商城提示框
	public static DWTextbox m_pet_leixing = null;
	public static DWTextbox m_pet_jingyan = null;
	public static DWListbox listBox01 =null;
	public static DWImageBox m_pet_xingxiang = null;
	public static DWTextbox m_pet_level = null;
	private DWTextbox m_pet_zhungbei = null;
	public static int index = 0;
	private Role role = null;

	public static DWAnimationBox m_pet_ab = null;
	public static Actor m_pet_actor = null;
	//c菜单窗口
	public static String UIPETMENU_LEIXING = null; //类型
	public static String UIPETMENU_ICON = null;
	public static int UIPETMENU_DENGJI = 0; //等级
	public static int UIPETMENU_JINGYAN = 0;//经验
	public static int UIPETMENU_ZUIDAJINGYAN = 0;//升级经验（当前等级，最大经验）
	public static String UIPETMENU_DONGHUA = null;//动画
	public static int UIPET_NUMMIN = 0;
	public static int UIPET_NUMMAX = 0;
	
	public static int UIPETMENU_ZENGJIA = 0;//增加经验
	public static int UIPETMENU_LILIANG = 0;//力量
	public static int UIPETMENU_ZHILI = 0;//智力
	public static int UIPETMENU_JIQIAO = 0;//技巧
	public static int UIPETMENU_MINJIE = 0;//敏捷

	public static int UIPETMENU_NEXTZENGJIA = 0;
	public static int UIPETMENU_NEXTLILIANG = 0;
	public static int UIPETMENU_NEXTZHILI = 0;
	public static int UIPETMENU_NEXTJIQIAO = 0;
	public static int UIPETMENU_NEXTMINJIE = 0;

	//bitmap
	private byte m_bm_close = 0;
	private byte m_bm_beijing01 = 1;
	private byte m_bm_beijing02 = 2;
	private byte m_bm_xingxiang = 3;
	private byte m_bm_jindu01 = 4;
	private byte m_bm_jindu02 = 5;
	private byte m_bm_button = 6;
	private static byte m_bm_gd = 7;
	private byte m_bm_shili = 8;
	private byte m_bm_suo = 9;
	private byte m_bm_icon = 10;

	//imagebox
	private byte m_ib_close = 0;
	private byte m_ib_xingxiang = 1;
	
	private boolean flag = false;

	private void petLook()
	{
		
//		if (DWControlsManager.getInstance().contains(m_pet_frame)) {
//			return;
//		} else 
//		{
//			if (m_pet_frame != null) {
//				DWControlsManager.getInstance().addControl(m_pet_frame);
//				return;
//			}
//		}
		role = new Role();
		if(DWGameManager.AdpaterType == 2){
			m_pet_frame = new DWFrame(DWFrame.FRAME_TYPE_FULL, true);
		}else{
			m_pet_frame = new DWFrame(740, 430);
		}
		m_pet_frame.setClickClose(false);
		m_pet_title = new DWTitle("", m_pet_frame);
		m_imageBox_title = new DWImageBox(m_bm_title);
		m_imageBox_title.setNearAnchor(m_pet_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		// 关闭窗口
		m_pet_close = new DWImageBox(m_pet_bm[m_bm_close]);
//		m_pet_close.setShowWideHigh(28, 28);
		m_pet_close.setTouchZoomIn(30, 30);
		m_pet_close.addListener(m_close);
		m_pet_close.setNearAnchor(m_pet_title, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 0,0);
		//背景
		m_pet_background = new DWBackground(740, 420);
		if(DWGameManager.AdpaterType == 2){
			m_pet_background.setShowWideHigh(m_pet_frame.getShowWidth()-10, m_pet_frame.getShowHeight() - 10);
		}
		m_pet_background.setShow(false);
		m_pet_background.setNearAnchor(m_pet_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		m_pet_frame.addControl(m_pet_background);
		
		//背景左
		m_background_left = new DWBackground(340, 400);
		if(DWGameManager.AdpaterType == 2){
			m_background_left.setShowWideHigh(DWGameManager.Screen_Width*34/80, DWGameManager.Screen_Height*40/48);
		}
		m_background_left.setFill(false);
		m_background_left.setNearAnchor(m_pet_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 10);
		m_pet_frame.addControl(m_background_left);
		//背景右
		m_background_right = new DWBackground(360, 400);
		if(DWGameManager.AdpaterType == 2){
			m_background_right.setShowWideHigh(DWGameManager.Screen_Width*36/80, DWGameManager.Screen_Height*40/48);
		}
		m_background_right.setFill(false);
		if(DWGameManager.AdpaterType == 2){
			m_background_right.setNearAnchor(m_background_left, Tools.LEFT_TOP, Tools.RIGHT_TOP, 50, 0);
		}else{
			m_background_right.setNearAnchor(m_pet_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 360, 10);
		}
		m_pet_frame.addControl(m_background_right);
		
		m_pet_zhungbei = new DWTextbox("<mark c=FFFF00>已装备</mark>");
		m_pet_zhungbei.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 0);
		m_pet_zhungbei.setShow(false);
		m_pet_zhungbei.setTouch(false);
		m_pet_frame.addControl(m_pet_zhungbei);
		
		m_pet_bt01 = new DWButton("收回", m_pet_bm[m_bm_button]);
		m_pet_bt01.setDownImage(m_bm_anxia);
		m_pet_bt01.addListener(m_pet_shouhui);
		if(DWGameManager.AdpaterType == 2){
			m_pet_bt01.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 30, 460);
		}else{
			m_pet_bt01.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 50, 360);
		}
		
		m_pet_bt02 = new DWButton("背包", m_pet_bm[m_bm_button]);
		m_pet_bt02.setDownImage(m_bm_anxia);
		if(DWGameManager.AdpaterType == 2){
			m_pet_bt02.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 170, 460);
		}else{
			m_pet_bt02.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 150, 360);
		}

		m_pet_shangcheng = new DWButton("商城", m_pet_bm[m_bm_button]);
		m_pet_shangcheng.setDownImage(m_bm_anxia);
		if(DWGameManager.AdpaterType == 2){
			m_pet_shangcheng.setNearAnchor(m_pet_bt02, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 170, 0);
		}else{
			m_pet_shangcheng.setNearAnchor(m_pet_bt02, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 100, 0);
		}
		m_pet_frame.addControl(m_pet_bt02);
		m_pet_frame.addControl(m_pet_shangcheng);
		
		refreshPet();
		refreshGrid();

		//标签
		m_pet_label = new DWLabel("选择宠物可以派遣出战");
		m_pet_label.setShowBackground(false);
		m_pet_label.setTextColor(Color.YELLOW);
		m_pet_label.setNearAnchor(m_pet_gd, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 50, 50);

		m_pet_frame.addControl(m_pet_title);
		m_pet_frame.addControl(m_imageBox_title);
		m_pet_frame.addControl(m_grid_beijing);
		m_pet_frame.addControl(m_pet_gd);
		m_pet_frame.addControl(m_pet_label);
		m_pet_frame.addControl(m_pet_close);


		DWControlsManager.getInstance().addControl(m_pet_frame);
		
	
		//背包按钮
		m_pet_bt02.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				UIWindows.getInstance().m_bag.open((byte) 0);
				UIWindows.getInstance().m_pet.close((byte)0);
			}
		});
		//商城按钮
		m_pet_shangcheng.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWGameManager.getInstance().getSendMessage().sendOpenVipShop((byte) 6, (byte) 0);
			}
		});

		//网格监听
		m_pet_gd.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
	
				
				index = m_pet_gd.getTouchIndex();
				if(index >= pets.length && UIPET_NUMMIN -1>=index ){
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "这里没有宠物");
//					Tools.debugPrintln("-----------------in");
					
				}else if(index < pets.length){
				Pet petclk = pets[index];
			
				UIPETMENU_LEIXING = petclk.name; //名字（类型）
				UIPETMENU_DENGJI = petclk.level;//等级
				UIPETMENU_JINGYAN = petclk.exp;//当前经验
				UIPETMENU_ZUIDAJINGYAN = petclk.maxExp;//升级所需经验
				if(m_pet_actor != null){
					m_pet_actor=null;
				}
				m_pet_actor = new Actor(petclk.m_anims[0]);//动画效果
				for (int i = 0; i < petclk.AttributeS.length; i++) {
					int type = petclk.AttributeS[i][0];
					switch (type) {
						case Pet.加经验:
							UIPETMENU_ZENGJIA = petclk.AttributeS[i][1];
							break;
						case Attribute.力:
							UIPETMENU_LILIANG = petclk.AttributeS[i][1];
							break;
						case Attribute.智:
							UIPETMENU_ZHILI = petclk.AttributeS[i][1];
							break;
						case Attribute.技:
							UIPETMENU_JIQIAO = petclk.AttributeS[i][1];
							break;
						case Attribute.敏:
							UIPETMENU_MINJIE = petclk.AttributeS[i][1];
							break;
						
					}
				}
				
				for (int i = 0; i < petclk.NextAttributeS.length; i++) {
					int type = petclk.NextAttributeS[i][0];
					switch (type) {
						case Pet.加经验:
							UIPETMENU_NEXTZENGJIA = petclk.NextAttributeS[i][1];
							break;
						case Attribute.力:
							UIPETMENU_NEXTLILIANG = petclk.NextAttributeS[i][1];
							break;
						case Attribute.智:
							UIPETMENU_NEXTZHILI = petclk.NextAttributeS[i][1];
							break;
						case Attribute.技:
							UIPETMENU_NEXTJIQIAO = petclk.NextAttributeS[i][1];
							break;
						case Attribute.敏:
							UIPETMENU_NEXTMINJIE = petclk.NextAttributeS[i][1];
							break;
						
					}
				}
				
				
				UIWindows.getInstance().m_petMenu.open((byte) 0);
				}
				else if(index > UIPET_NUMMIN -1){//宠物扩背包
					final DWMessageBox m_messageBox = new DWMessageBox("背包扩容", "确定花费10元宝开启1个格子？");
					m_pet_frame.addControl(m_messageBox);
					m_messageBox.addListener(new DWListener() {
						
						public void OnDoubleClick() {
						
						}
						
						public void OnClick() {
							if(m_messageBox.getResult() == DWMessageBox.SOFTKEY_OK){
								DWGameManager.getInstance().getSendMessage().sendChongWuKuoBeiBao();
								m_pet_frame.removeControl(m_messageBox);
							}else if(m_messageBox.getResult() == DWMessageBox.SOFTKEY_BACK){
								m_pet_frame.removeControl(m_messageBox);
							}
						}
					});
					
				}//if
			}
		});

		m_pet_gd.addDragListener(new DWDragListener() {
			public void OnDrag(DWControl control)
			{
				if (control instanceof DWGrids) {// 是不是网格
					DWGrids dgs = (DWGrids) control;
					if (dgs.equals(m_pet_gd)) {// 是不是同一个网格
						Tools.debugPrintln("位置 : " + m_pet_gd.getTouchIndex() + " : "
								+ dgs.getDragIndex());
						if (m_pet_gd.getTouchIndex() == dgs.getDragIndex()) {// 在同一个位置
							Tools.debugPrintln("同一个位置,不操作!");
						}
						else {// 不同位置,放置新位置
							dgs.exchangeGrid(dgs.getDragIndex(),
									m_pet_gd.getTouchIndex());
						}
					}
				}
			}
		});	
		
	}
	//关闭按钮
    private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
//			UIMain.open_flag = false;
			UIWindows.getInstance().m_pet.close((byte)0);
//			DWControlsManager.getInstance().removeControl(m_pet_frame);
		}
	};

	private DWListener m_pet_shouhui = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			outer:
				for(int i=0; i<pets.length;i++){
					if(pets[i].isChuZhan == true ){
						Long a = pets[i].guid;
						DWGameManager.getInstance().getSendMessage().sendChongWuChuZhan((byte)0, a);
						Tools.debugPrintln("--------now  shouhui zhan-----"+i);
						break outer;
					}
				}		
			Long a = pets[index].guid;
			DWGameManager.getInstance().getSendMessage().sendChongWuChuZhan((byte)0, a);
		}
	};
	
//	//删除
//	public static void petDelete(int key)
//	{
//		switch (key) {
//			case 0:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 1:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 2:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 3:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 4:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 5:
////			m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
////			break;
//			case 6:
//				m_pet_gd.setGridImage(m_pet_bm[m_bm_gd], key);
//				break;
//			case 7:
//				break;
//			case 8:
//				break;
//			case 9:
//				break;
//			case 10:
//				break;
//
//		}
//	}
	
	private void refreshGrid(){
		m_pet_frame.removeControl(m_pet_gd);
		m_pet_frame.removeControl(m_grid_beijing);
		UIPET_NUMMIN = 0;
		UIPET_NUMMAX = 0;
		m_pet_gd = null;
		m_grid_beijing = null;
		//网格
		UIPET_NUMMIN = DWGameManager.getInstance().m_role.petCellMin;
		UIPET_NUMMAX = DWGameManager.getInstance().m_role.petCellMax;
		m_pet_gd = new DWGrids(m_pet_bm[m_bm_gd], UIPET_NUMMAX, 5, 4);
		m_pet_gd.setDrag(true);
		m_pet_gd.setInterspace(3, 2);
		if(DWGameManager.AdpaterType == 2){
			m_pet_gd.setNearAnchor(m_background_right, Tools.LEFT_TOP, Tools.LEFT_TOP, 30, 50);
		}else{
			m_pet_gd.setNearAnchor(m_background_right, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 20);
		}

		//衬格子的底图格子
		m_grid_beijing = new DWGrids(m_pet_bm[m_bm_gd], UIPET_NUMMAX, 5, 4);
		m_grid_beijing.setDrag(true);
		m_grid_beijing.setInterspace(3, 2);
		m_grid_beijing.setNearAnchor(m_pet_gd, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
				
		//未开启格子
		if(UIPET_NUMMIN != UIPET_NUMMAX){
			m_pet_gd.setGridsImage(m_pet_bm[m_bm_suo], UIPET_NUMMIN,UIPET_NUMMAX-1);
		}
		//接受的宠物图片
		if(pets != null){
			for(int i = 0; i< pets.length;i++){
				m_pet_tubiao[i] = IconManger.getInstance().getIcon(pets[i].lcon);
				m_pet_gd.setGridImage(m_pet_tubiao[i], i);
			}
		}
		m_pet_frame.addControl(m_pet_gd);
		m_pet_frame.addControl(m_grid_beijing);
	}
	
	private void refreshPet(){
		
		m_pet_frame.removeControl(m_pet_shangchengtishi);
		m_pet_frame.removeControl(listBox);
		m_pet_frame.removeControl(listBox01);
		m_pet_frame.removeControl(m_pet_bt01);
		m_pet_frame.removeControl(m_pet_jingyan);
		m_pet_frame.removeControl(m_pet_level);
		m_pet_frame.removeControl(m_text_next);
		m_pet_frame.removeControl(m_pet_leixing);
		m_pet_frame.removeControl(m_pet_xingxiang);
		m_pet_frame.removeControl(m_pet_ab);
		
		m_pet_shangchengtishi = null;
		m_pet_actor = null;
		m_pet_ab = null;
		m_pet_level = null;
		m_pet_jingyan = null;
		m_text_next = null;
		m_pet_leixing = null;
		listBox = null;
		listBox01 = null;
		m_pet_xingxiang = null;
		
		int uu = 0;
		//右侧
		if(pets.length == 0){
			m_pet_shangchengtishi = new DWTextbox(
					"商城中可以购买宠物，宠物可以陪伴你，给你属性加成", 200, 150);
			m_pet_shangchengtishi.setNearAnchor(m_pet_title,
					Tools.LEFT_TOP, Tools.LEFT_TOP, 80, 130);
			m_pet_zhungbei.setShow(false);
			m_pet_zhungbei.setTouch(false);
//			m_pet_kuang = new DWImageBox(m_bm_kuang);
//			m_pet_kuang.setShowWideHigh(250, 160);
//			m_pet_kuang.setNearAnchor(m_pet_shangchengtishi, Tools.LEFT_TOP, Tools.LEFT_TOP, -25, -12);
//			
//			m_pet_frame.addControl(m_pet_kuang);
			m_pet_frame.addControl(m_pet_shangchengtishi);
			m_pet_frame.removeControl(listBox);
			m_pet_frame.removeControl(listBox01);
			m_pet_frame.removeControl(m_pet_bt01);
			m_pet_frame.removeControl(m_pet_jingyan);
			m_pet_frame.removeControl(m_pet_level);
			m_pet_frame.removeControl(m_text_next);
			m_pet_frame.removeControl(m_pet_leixing);
			m_pet_frame.removeControl(m_pet_xingxiang);
			
		}else {
			outer:
		for (int i = 0; i < pets.length; i++) {
			if (pets[i].isChuZhan == true) {
				//动画
				if(m_pet_actor != null){
					m_pet_actor =null;
				}
				m_pet_actor = new Actor(pets[i].m_anims[0]);
				m_pet_ab = new DWAnimationBox(m_pet_actor);
				m_pet_ab.setAction(2);
				m_pet_ab.setNearAnchor(m_pet_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 50, 30);

				m_pet_level = new DWTextbox("等级 :"+pets[i].level);
				m_pet_level.setNearAnchor(m_pet_ab, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, -20, 10);
				//textbox
				m_pet_jingyan = new DWTextbox("经验值:"+pets[i].exp+"/"+pets[i].maxExp+" ");
				m_pet_jingyan.setNearAnchor(m_pet_level, Tools.LEFT_TOP, Tools.RIGHT_TOP, 20, 0);
				
				m_text_next = new DWTextbox("下一等级：");
				m_text_next.setNearAnchor(m_pet_level, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
				
				m_pet_zhungbei.setShow(true);
				m_pet_zhungbei.setTouch(false);
				
				
				m_pet_leixing = new DWTextbox("名字"+"："+pets[i].name);
				if(DWGameManager.AdpaterType == 2){
					m_pet_leixing.setNearAnchor(m_pet_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 160, 30);
				}else{
					m_pet_leixing.setNearAnchor(m_pet_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 160, 20);
				}
				
				
				Bitmap m_chongwu = IconManger.getInstance().getIcon(pets[i].lcon);
//				UIMain.m_iamgeBox_chongwu.setImage(m_chongwu);
				UIWindows.getInstance().m_main.m_iamgeBox_chongwu.setImage(m_chongwu);

				// 当前属性
				
				listBox = new DWListbox(180, 110);
				listBox.setNearAnchor(m_pet_leixing, Tools.LEFT_TOP,
						Tools.LEFT_BOTTOM, 0, 10);
				listBox.removeAllSubItem();
				for (int j = 0; j < pets[i].AttributeS.length; j++) {
					int type = pets[i].AttributeS[j][0];
					DWListSubItem lsi = new DWListSubItem();
					DWTextbox textBox = new DWTextbox(Pet.ATTRIBUTE_NAME[type]
							+":"+ pets[i].AttributeS[j][1]);
					if(type == 0){
						textBox.setText(Pet.ATTRIBUTE_NAME[type]
							+":"+ pets[i].AttributeS[j][1]+"%");
					}
					lsi.addControls(textBox);
					textBox.setNearAnchor(lsi, Tools.LEFT_TOP, Tools.LEFT_TOP,
							0, 0);
					listBox.addSubItem(lsi);

				}
				
				listBox01 = new DWListbox(180, 90);
				listBox01.removeAllSubItem();
				listBox01.setNearAnchor(m_text_next, Tools.LEFT_TOP,
						Tools.LEFT_BOTTOM, 0, 10);
				for (int j = 0; j < pets[i].NextAttributeS.length; j++) {
					int type = pets[i].NextAttributeS[j][0];
					DWListSubItem lsi = new DWListSubItem();
					DWTextbox textBox = new DWTextbox(Pet.ATTRIBUTE_NAME[type]
							+":"+ pets[i].NextAttributeS[j][1]);
					if(type == 0){
						textBox.setText(Pet.ATTRIBUTE_NAME[type]
								+":"+ pets[i].NextAttributeS[j][1]+"%");
					}
					lsi.addControls(textBox);
					textBox.setNearAnchor(lsi, Tools.LEFT_TOP, Tools.LEFT_TOP,
							0, 0);
					listBox01.addSubItem(lsi);

				}
				
				m_pet_frame.addControl(m_pet_bt01);
				m_pet_frame.addControl(m_pet_shangcheng);
				m_pet_frame.addControl(listBox);
				m_pet_frame.addControl(listBox01);
				m_pet_frame.addControl(m_pet_leixing);
				m_pet_frame.addControl(m_pet_jingyan);
				m_pet_frame.addControl(m_pet_level);
				m_pet_frame.addControl(m_text_next);
				m_pet_frame.addControl(m_pet_xingxiang);
				m_pet_frame.removeControl(m_pet_shangchengtishi);
//				m_pet_frame.removeControl(m_pet_kuang);
				m_pet_frame.addControl(m_pet_ab);
				
				
				break outer;
			} else if(pets[i].isChuZhan == false){
				if(uu == 0){
					// 商城购买宠物提示
					m_pet_shangchengtishi = new DWTextbox(
							"商城中可以购买宠物，宠物可以陪伴您整个冒险旅程，并为您提供各种属性", 200, 150);
					m_pet_shangchengtishi.setNearAnchor(m_pet_title,
							Tools.LEFT_TOP, Tools.LEFT_TOP, 80, 130);
					m_pet_zhungbei.setShow(false);
					m_pet_zhungbei.setTouch(false);
//					m_pet_kuang = new DWImageBox(m_bm_kuang);
//					m_pet_kuang.setShowWideHigh(250, 160);
//					m_pet_kuang.setNearAnchor(m_pet_shangchengtishi, Tools.LEFT_TOP, Tools.LEFT_TOP, -25, -12);
//					
//					m_pet_frame.addControl(m_pet_bt02); && UIPetMenu.flag == false
//					m_pet_frame.addControl(m_pet_shangcheng);
//					m_pet_frame.addControl(m_pet_kuang);
					m_pet_frame.addControl(m_pet_shangchengtishi);
					
//					UIMain.m_iamgeBox_chongwu.setImage(UIMain.m_bm_chongwu);
					UIWindows.getInstance().m_main.m_iamgeBox_chongwu.setImage(UIWindows.getInstance().m_main.m_bm_chongwu);
					
					m_pet_frame.removeControl(listBox);
					m_pet_frame.removeControl(listBox01);
					m_pet_frame.removeControl(m_pet_bt01);
					m_pet_frame.removeControl(m_pet_jingyan);
					m_pet_frame.removeControl(m_pet_level);
					m_pet_frame.removeControl(m_text_next);
					m_pet_frame.removeControl(m_pet_leixing);
					m_pet_frame.removeControl(m_pet_xingxiang);
				}
//				
				uu=1;
//				break;
				
				
			}// else
		}
	}
	}
	//根据id得到格子
	public DWGrid m_grid= null;
	public DWGrid getGrid(String key){
		
	if(DWControlsManager.getInstance().contains(m_pet_frame) == false){
			return null;
		}else{
			if(pets != null){
				for(int i=0;i<pets.length;i++){
					if(pets[i].key.equals(key)){				
						return m_pet_gd.m_grids.get(i);
					}
				}
			}
		}
				
		return null;
	}
	public void init()
	{
		m_pet_bm[m_bm_close] = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");//返回键（退出）
		m_bm_title = UIWindows.createImage("/img/newui/chongwu_1.gnp");
//		m_pet_bm[m_bm_beijing02] = UIWindows.createImage("/img/newui/liaotianxbjk_2.gnp");
//		m_pet_bm[m_bm_xingxiang] = UIWindows.createImage("/img/newui/beibaocw_1.gnp");
		m_pet_bm[m_bm_button] = UIWindows.createImage("/img/newui/anniu_2.gnp");//按钮图片
		m_bm_anxia = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		m_pet_bm[m_bm_gd] = UIWindows.createImage("/img/newui/wupinlan.gnp");//格子底图
//		m_pet_bm[m_bm_shili] = UIWindows.createImage("/img/newui/big_2.gnp");
		m_pet_bm[m_bm_suo] = UIWindows.createImage("/img/newui/beibaost_1.gnp");//带锁的格子底图
//		m_backgrpund_bitmap = UIWindows.createImage("/img/newui/wodeduiwubk_1.gnp");
		m_bm_kuang = UIWindows.createImage("/img/newui/wupinlan.gnp");//左侧文字底图

	}

	public void open(byte index)
	{
		if(flag == false){
			UIWindows.getInstance().m_bag.close((byte)0);
			petLook();
			flag = true;
		}else{
			if(m_pet_frame != null){
				DWControlsManager.getInstance().addControl(m_pet_frame);
				refreshGrid();
			}else{
				return;
			}
			
		}
		
		
	}

	public void close(byte index)
	{
		flag = false;
		DWControlsManager.getInstance().removeControl(m_pet_frame);
		UIWindows.getInstance().m_zhanli.open_other(DWGameManager.getInstance().m_role.m_zhanli);//战力相关
	}

	public void dispose()
	{
	}

	public void recvPetOpenWin(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_宠物);
		byte petNum = dis.readByte(); //宠物个数		
		pets = new Pet[petNum];
		for (int i = 0; i < petNum; i++) {
			Pet pet = new Pet();
			pet.isChuZhan = (dis.readByte()==0?false:true); //是否出战
			Tools.debugPrintln("是否出战--------》"+pet.isChuZhan);
			pet.name = dis.readUTF(); //名字
			pet.level = dis.readByte(); // 等级
			pet.exp = dis.readInt(); // 当前经验
			pet.maxExp = dis.readInt(); //升级经验
			pet.guid = dis.readLong(); //id
			pet.key = dis.readUTF();
			pet.lcon = dis.readShort(); //图标url
//			pet.animation = dis.readUTF();
//			Tools.debugPrintln("动画路径"+pet.animation);
			pet.animation = new StringBuffer(DWSerializableFactory.DIR_ANIM).append(dis.readUTF()).append(DWSerializableFactory.EXTENSION_ANIM).toString();// 宠物动画路径
			Tools.debugPrintln("动画路径"+pet.animation);
			pet.m_refsResHead = new ResHead[1];
			pet.m_refsResHead[0] = new ResHead(DWSerializableFactory.RESOURCE_TYPE_ANIM, pet.animation, 1);
			pet.m_anims = new Animation[1];
			pet.m_anims[0] = (Animation)ResourceManager.getInstance().loadResource(DWGameManager.getInstance().m_resourceDynamic, pet.m_refsResHead[0]);
//			pet.setActionFrame(actionIDNext, frameID);
//			ResHead head = null;
//			Animation anim = 
//			pet.animation = dis.readUTF(); 
//			pet.m_anims = new Animation[1];
//			ResHead head = new ResHead(DWSerializableFactory.RESOURCE_TYPE_ANIM, pet.animation, 1);
//			ResourceManager.getInstance().loadResource(DWGameManager.getInstance().m_resourceStatic, head);
			byte currentAttributeSize = dis.readByte(); //当前属性个数
			pet.AttributeS = new int[currentAttributeSize][2];
			for (int j = 0; j < currentAttributeSize; j++) {
				pet.AttributeS[j][0] = dis.readByte(); //属性类型
				pet.AttributeS[j][1] = dis.readInt(); //属性值
			}
			byte nextAttributeSize = dis.readByte(); //  下个等级属性个数
			pet.NextAttributeS = new int[nextAttributeSize][2];
			for (int j = 0; j < nextAttributeSize; j++) {
				pet.NextAttributeS[j][0] = dis.readByte(); //属性类型
				pet.NextAttributeS[j][1] = dis.readInt(); //属性值
			}
			pets[i] = pet;
		}

		UIWindows.getInstance().m_pet.open((byte) 0);
	}

	/**
	 * 宠物出战或收回
	 * @param dis
	 * @throws IOException
	 */
	public void recvPetCZorSH(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_宠物);
		byte state = dis.readByte(); //0 收回成功  1放出成功
		long guid = dis.readLong(); //
		short icon = dis.readShort();
		if (state == 0) {
			int size = pets.length;
			for (int i = 0; i < size; i++) {
				if (guid == pets[i].guid) {
					pets[i].isChuZhan = false;		
				}
			}
			refreshPet();
		}
		else if (state == 1) {
		    if (pets == null) {
                pets = new Pet[1];
                Pet pet = new Pet();
                pets[0] = pet;
                pet.AttributeS = new int[0][0];
                pet.lcon = icon;
                pet.guid = guid;
            }
			int size = pets.length;
			for (int i = 0; i < size; i++) {
				if (guid == pets[i].guid) {
					pets[i].isChuZhan = true;
					Bitmap m_chongwu = IconManger.getInstance().getIcon(pets[i].lcon);
					UIWindows.getInstance().m_main.m_iamgeBox_chongwu.setImage(m_chongwu);
								
				}else{
					pets[i].isChuZhan = false;
				}
			}
			if (!flag) {
	            return;
	        }		
			refreshPet();	
		}
		else {
//			DWGameManager.getInstance().showToast("", time)
		}
	}
		/**
		 *  接收背包格数
		 * @param dis
		 * @throws IOException
		 */
		public void recvPetBagNum(DataInputStream dis) throws IOException
		{
			DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_宠物);
			byte state = dis.readByte(); //0 收回成功  1放出成功
			Tools.debugPrintln("开启宠物背包格数 state ："+state);
			switch (state) {
				case 0:
					break;
				case 1:
					DWGameManager.getInstance().m_role.petCellMin=dis.readByte();
					DWGameManager.getInstance().m_role.petCellMax=dis.readByte();
					//刷一下界面
					UIWindows.getInstance().m_pet.close((byte)0);
					UIWindows.getInstance().m_pet.open((byte)0);
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "开启背包成功");
					break;
				case 2:
					DWGameManager.getInstance().showToast("元宝不足！", Toast.LENGTH_SHORT);
					//消息确认框
					m_pet_messageBox = new DWMessageBox("友情提示", "您的元宝不足，是否去立即去充值");
					m_pet_messageBox.addListener(new DWListener() {
						
						public void OnDoubleClick() {
						}
						
						public void OnClick() {
							if(m_pet_messageBox.getResult() == DWMessageBox.SOFTKEY_OK){
								DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "功能尚未开放！");
								m_pet_frame.removeControl(m_pet_messageBox);
							}else if(m_pet_messageBox.getResult() == DWMessageBox.SOFTKEY_BACK){
								m_pet_frame.removeControl(m_pet_messageBox);
							}
							
						}
					});
					m_pet_frame.addControl(m_pet_messageBox);
					break;
				case 3:
					DWGameManager.getInstance().m_role.petCellMin=dis.readByte();
					DWGameManager.getInstance().m_role.petCellMax=dis.readByte();
					break;
			}

		}

	public static Pet[] pets;

	//判断是否在容器中
	public DWImageBox isNewImageBox(){
		if(DWControlsManager.getInstance().contains(m_pet_close) == false){
			return null;
		}else
			return m_pet_close;
	}

	
	public void logic()
	{
		// TODO Auto-generated method stub
		
	}
}
